/**
 * 
 */
package com.nexttwist.adapters;

import java.util.List;

import android.content.Context;
import android.media.MediaPlayer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.widget.ProfilePictureView;
import com.nexttwist.R;
import com.nexttwist.auth.AuthenticationHandler;
import com.nexttwist.model.SampleTrack;
import com.nexttwist.util.MyMediaPlayer;

/**
 * @author HarishKumarG
 *
 */
public class DashboardAdapter extends ArrayAdapter<SampleTrack> {
	
	private final Context context;
	private final List<SampleTrack> data;

	public DashboardAdapter(Context context,List<SampleTrack> objects) {
		super(context, R.layout.list_item, objects);
		this.context = context;
		this.data = objects;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = mInflater.inflate(R.layout.list_item, parent, false);
		ProfilePictureView mProfilePicture = (ProfilePictureView) rowView.findViewById(R.id.profile_picture);
		TextView mAlbumTitle = (TextView) rowView.findViewById(R.id.album_title);
		TextView mTrackTitle = (TextView) rowView.findViewById(R.id.track_title);
		final ImageButton mBtnPlay = (ImageButton) rowView.findViewById(R.id.btn_play);
		final ImageButton mBtnPause = (ImageButton) rowView.findViewById(R.id.btn_pause);
		
		SampleTrack track = data.get(position);
		
		mAlbumTitle.setText(track.getAlbum());
		mTrackTitle.setText(track.getTitle());
		
		mProfilePicture.setCropped(true);
		mProfilePicture.setProfileId(AuthenticationHandler.getInstance().getmGraphUser().getId());
		
		final int fPosition = position;
		mBtnPlay.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				SampleTrack mTrack = data.get(fPosition);
				MyMediaPlayer.getInstance().play(mTrack.getTrackUrl());
				mBtnPlay.setVisibility(View.GONE);
				mBtnPause.setVisibility(View.VISIBLE);
			}
		});
		
		mBtnPause.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				MyMediaPlayer.getInstance().pause();
				mBtnPlay.setVisibility(View.VISIBLE);
				mBtnPause.setVisibility(View.GONE);
			}
		});
		
		return rowView;
	}
	
}
